package com.javacoo.xservice.base.support.lock;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import com.javacoo.xkernel.spi.ExtensionLoader;
import com.javacoo.xservice.base.support.lock.config.LockConfig;

import lombok.extern.slf4j.Slf4j;

/**
 * 锁接口工厂
 * <li></li>
 *
 * @author: duanyong@jccfc.com
 * @since: 2021/3/16 8:56
 */
@Slf4j
@Component
@ConditionalOnBean(LockConfig.class)
public class LockFactory {
    /** 锁配置 */
    @Autowired
    private LockConfig lockConfig;

    @Bean
    public Lock createLock() {
        log.info("初始化分布式锁,实现类名称:{}",lockConfig.getImpl());
        LockHolder.lock = ExtensionLoader.getExtensionLoader(Lock.class).getExtension(lockConfig.getImpl());
        log.info("初始化分布式锁成功,实现类:{}",LockHolder.lock);
        return LockHolder.lock;
    }
}
